<template>
    <div>
        <!-- 面包屑导航区 -->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 内容卡片区 -->
        <el-card>
            <!-- 搜索区 -->
            <el-row>
                <el-col :span="8">
                    <el-input placeholder="请输入内容" v-model="queryInfo.query" width="100px" clearable
                        @clear="getGoodsList">
                        <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="12">
                    <el-button type="primary" @click="goAddPage">添加商品</el-button>
                </el-col>
            </el-row>
            <!-- 表格展示区 -->
            <el-table :data="goodsList" border style="width: 100%">
                <el-table-column type="index" label="#" width="40"></el-table-column>
                <el-table-column prop="goods_name" label="商品名称"></el-table-column>
                <el-table-column prop="goods_price" label="商品价格(元)" width="95"></el-table-column>
                <el-table-column prop="goods_weight" label="商品重量" width="70"></el-table-column>
                <el-table-column label="创建时间" width="140">
                    <template slot-scope="scope">
                        {{ scope.row.add_time | dateFormat }}
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="130">
                    <template slot-scope="scope">
                        <el-button size="mini" icon="el-icon-edit" type="primary"
                            @click="showEditDialog(scope.row.goods_id)"></el-button>
                        <el-button size="mini" icon="el-icon-delete" type="danger"
                            @click="deleteGoods(scope.row.goods_id)"></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页展示区 -->
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum" :page-sizes="[2, 5, 10, 20]" :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </el-card>
    </div>
</template>

<script>
export default {
    data() {
        return {
            goodsList: [],
            total: 0,
            queryInfo: {
                query: '',
                pagenum: 1,
                pagesize: 10
            }
        }
    },
    created() {
        this.getGoodsList()
    },
    methods: {
        getGoodsList() {
            this.$http({
                url: 'goods',
                method: 'get',
                params: this.queryInfo
            }).then(res => {
                if (res.meta.status === 200) {
                    // console.log(res.data);
                    this.goodsList = res.data.goods
                    this.total = res.data.total
                } else {
                    this.$message.error(res.meta.msg)
                }
            })
        },
        handleSizeChange(val) {
            this.queryInfo.pagesize = val
            this.getGoodsList()
        },
        handleCurrentChange(val) {
            this.queryInfo.pagenum = val
            this.getGoodsList()
        },
        deleteGoods(goods_id) {
            this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http({
                    url: `goods/${goods_id}`,
                    method: 'delete',
                }).then(res => {
                    if (res.meta.status === 200) {
                        this.$message.success(res.meta.msg)
                        this.getGoodsList()
                    } else {
                        this.$message.error(res.meta.msg)
                    }
                })

            }).catch(() => {
                this.$message.info('已取消删除');
            });
        },
        goAddPage() {
            this.$router.push('/goods/add')
        }
    }
}
</script>

<style scoped>
.el-col + .el-col {
    margin-left: 20px;
}
</style>